//获取全局APP
var app = getApp();
var helper = require('../../utils/helper.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: ['../../static/ico/timg.gif'],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    isloading: true,
    newsInfo: ['第三方斯蒂芬给对方水电费',
      '打发士大夫就圣诞节六块腹肌',
      '就看电视了荆防颗粒时代峻峰'
    ],
    host: app.globalData.remoteDomain,
    myhost: app.globalData.remoteDomainMy,
    booktop3: [],
    newbook: [],
    starbook: {},
    starmusic: {},
    starmovie: {},
    musictop6: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //this
    var that = this;

    //网络请求，给轮播提供数据
    wx.request({
      url: app.globalData.remoteDomainMy + '/api/swiper.php',
      success: function(res) {
        //拿到网络数据，渲染模板
        that.setData({
          isloading: false,
          imgUrls: res.data
        })
      }
    })
  },
  onShow: function() {
    var that = this;
    // 显示tabbar购物车的数量
    //获取openid
    let openid = wx.getStorageSync('openid');

    //获取购买产品的数据
    let storageData = wx.getStorageSync(openid)

    //获取购买总数量
    let cartCounts = helper.cartCount(this, storageData);

    if (cartCounts > 0) {
      //设置tabBar数量
      wx.setTabBarBadge({
        index: 2,
        text: cartCounts.toString(),
      })
    }



    //畅销书
    wx.request({
      url: app.globalData.remoteDomainApi + 'booksaletop3.php',
      data: {
        x: '',
        y: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.length > 0) {
          that.setData({
            booktop3: res.data
          })
        }
      }
    })


    //新书+特价包邮
    wx.request({
      url: app.globalData.remoteDomainApi + 'newbook.php',
      data: {
        x: '',
        y: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.length > 0) {
          that.setData({
            newbook: res.data
          })
        }
      }
    })


    //五星排行榜
    wx.request({
      url: app.globalData.remoteDomainApi + 'star5.php',
      data: {
        x: '',
        y: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data) {
          if (res.data.book) {
            that.setData({
              starbook: res.data.book
            })
          }
          if (res.data.music) {
            that.setData({
              starmusic: res.data.music,
            })
          }
          if (res.data.movie) {
            that.setData({
              starmovie: res.data.movie
            })
          }
        }
      }
    })

    //音乐top6
    wx.request({
      url: app.globalData.remoteDomainApi + 'musictop6.php',
      data: {
        x: '',
        y: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.length > 0) {
          that.setData({
            musictop6: res.data
          })

        }
      }
    })
  },
/**
 * 畅销书排行榜
 */
  showMoreBookTop3:function(){
    wx.navigateTo({
      url: '../listbook/listbook?tap=bookmoretop',
    })
  },

  //跳转
  gobooklist: function(e) {
    //获取标签参数
    let tap = e.currentTarget.dataset.tap
    //页面跳转
    wx.navigateTo({
      url: '../listbook/listbook?tap=' + tap,
    })
  },

  /**
   * 详情页
   */
  gourl: function(e) {
    //获取url
    let url = e.currentTarget.dataset.url;
    let res_home = url.indexOf('home');
    let res_scan = url.indexOf('scan');
    let res_cart = url.indexOf('cart');
    let res_my = url.indexOf('my');
    if (res_home > -1 || res_scan > -1 || res_cart > -1 || res_my > -1) {
      //跳转
      wx.switchTab({
        url: '../' + url,
      })
    } else {
      //执行跳转
      wx.navigateTo({
        url: '../' + url,
      })
    }
  },

  /**
   * 跳转音乐
   */
  goList: function(e) {
    //获取标签参数
    let tap = e.currentTarget.dataset.tap
    //页面跳转
    wx.navigateTo({
      url: '../' + tap + '/' + tap,
    })
  },

  /**
   * 畅销书TOP3跳转到图书详情
   */

  goDetail: function(e) {
    //获取pid
    let pid = e.currentTarget.id;

    wx.navigateTo({
      url: '../detailBook/detailBook?id=' + pid,
    })
  }
})